package com.example.yang.yixin.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.yang.yixin.Adapter.PersonAdapter;
import com.example.yang.yixin.R;
import com.example.yang.yixin.Socket.Signset;
import com.example.yang.yixin.Utils.Person_info;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import com.example.yang.yixin.Socket.Signset;

public class PersonActivity extends BaseActivity {
    View v;
    private List<Person_info> Person_info=new ArrayList<>();
    private List<String> list=new ArrayList<>();
    private List<String> list1=new ArrayList<>();
    private RecyclerView mRecyclerView;
    private PersonAdapter madpater;
    private SmartRefreshLayout smartRefreshLayout;
    private ImageView mIv_back;
    SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);

        pref=getSharedPreferences("user",MODE_PRIVATE);

        initview();
        getServerdata();
        getPreData();
        getEarlyData();
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000);
                Person_info.clear();
                getServerdata();

            }
        });
    }

    private void getPreData() {
        final String username=pref.getString("username",null);
            String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/CountServlet?bkpassword="+getIntent().getStringExtra("bkpassword")+"&username="+username;
            RequestQueue mQueue = Volley.newRequestQueue(this);


            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        String  Numberone,Numbertwo,Numberthree;
                        Numberone =new String(response.getString("one"));
                        Numbertwo =new String(response.getString("two"));
                        Numberthree =new String(response.getString("three"));
                        list.add(Numberone);
                        list.add(Numbertwo);
                        list.add(Numberthree);


                    } catch (JSONException e) {
                        Log.e("TheMost发生错误", e.getMessage());
                    }
                }
            },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Log.e("TAG错误", volleyError.getMessage(), volleyError);
                        }
                    }) {
                protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                    try {
                        JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                        return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                    } catch (JSONException je) {
                        return Response.error(new ParseError(je));
                    } catch (UnsupportedEncodingException e) {
                        return Response.error(new ParseError(e));
                    }

                }
            };


            mQueue.add(jsonObjectRequest);


        }



    private void getEarlyData() {
        final String username=pref.getString("username",null);
        String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/EarlyorLateServlet?bkpassword="+getIntent().getStringExtra("bkpassword")+"&username="+username;
        RequestQueue mQueue = Volley.newRequestQueue(this);


        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    String  mostearly,onetothree,other,controlmost,controlonetothree,controlother;
                    mostearly =new String(response.getString("mostearly"));
                    onetothree =new String(response.getString("onetothree"));
                    other =new String(response.getString("other"));
                    controlmost=response.getString("controlmost");
                    controlonetothree=response.getString("controlonetothree");
                    controlother=response.getString("controlother");
                    list1.add(mostearly);
                    list1.add(onetothree);
                    list1.add(other);
                    list.add(controlmost);
                    list.add(controlonetothree);
                    list.add(controlother);
                } catch (JSONException e) {
                    Log.e("TheMost发生错误", e.getMessage());
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG错误", volleyError.getMessage(), volleyError);
                    }
                }) {
            protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                    return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                } catch (JSONException je) {
                    return Response.error(new ParseError(je));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                }

            }
        };


        mQueue.add(jsonObjectRequest);


    }



    private void getServerdata() {
        String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/SelectpersonServlet?bkpassword="+getIntent().getStringExtra("bkpassword")+"&flag=person";
        RequestQueue mQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {

                    String username;
                    String cname;
                    String name;
                    int size=Integer.parseInt(response.getString("size")) ;
                    for (int i=0;i<size;i++) {
                        /*
                        这个地方要修改
                         */
                        username = new String(response.getString("username"+i));
                        name = new String(response.getString("name"+i));
                        cname = new String(response.getString("classname"+i));

                        Person_info a = new Person_info(username, name,cname);
                        Person_info.add(a);
                    }
                    madpater=new PersonAdapter(Person_info);

                    mRecyclerView.setAdapter(madpater);

                } catch (JSONException e) {

                    Log.e("json错误",e.getMessage());
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG错误", volleyError.getMessage(), volleyError);
                    }
                }) {
            protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                    return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                }catch (JSONException je) {
                    return Response.error(new ParseError(je));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                }

            }
        };


        mQueue.add(jsonObjectRequest);

    }



    private void initview() {
        initNarvBar(true,"成员",true);
        initBottombar(v);
        mRecyclerView=findViewById(R.id.recyclerView2);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        smartRefreshLayout=findViewById(R.id.smart_refresh2);

        mIv_back=findViewById(R.id.iv_back);
        mIv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(PersonActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });


    }

    public void SignClick(View view) {
        /*
        实现课堂签到的跳转
         */
        /*
        写课堂签到的界面
        1.人脸识别（防止代替签到）
        2.百度地图（防止距离过远）
         */
        String bkpassword= getIntent().getStringExtra("bkpassword");
        Intent intent=new Intent();
        intent.putExtra("bkpassword",bkpassword);
        intent.setClass(PersonActivity.this,SignInActivity.class);
        startActivity(intent);
    }

    public void QuestionClick(View view) {
        /*
        实现统计的跳转
         */
        Intent intent=new Intent();
        String one=list.get(0);
        String two=list.get(1);
        String three=list.get(2);
        String mostearly=list1.get(0);
        String onetothree=list1.get(1);
        String other=list1.get(2);
        String controlmost=list.get(3);
        String controlonetothree=list.get(4);
        String controlother=list.get(5);
        Log.d("mostearly",mostearly);
        Log.d("onetothree",onetothree);
        Log.d("other",other);
        Log.d("one",one);
        Log.d("two",two);//为什么还是0
        Log.d("three",three);
        Log.d("controlmost",controlmost);
        Log.d("controlonetothree",controlonetothree);//为什么还是0
        Log.d("controlother",controlother);
        intent.putExtra("one",one);
        intent.putExtra("two",two);
        intent.putExtra("three",three);
        intent.putExtra("mostearly",mostearly);
        intent.putExtra("onetothree",onetothree);
        intent.putExtra("other",other);
        intent.putExtra("controlmost",controlmost);
        intent.putExtra("controlonetothree",controlonetothree);
        intent.putExtra("controlother",controlother);
        intent.setClass(PersonActivity.this,QuestionActivity.class);
        startActivity(intent);


    }

    public void PhoneControlClick(View view) {
        /*
         控制手机页面的跳转
         */
        Intent intent = new Intent();
        String username = getIntent().getStringExtra("data");
        intent.putExtra("data", username);
        intent.setClass(PersonActivity.this, PhoneControlActivity.class);
        startActivity(intent);


    }



}
